package nl.nhl.idp.Views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class MazeSensorView extends View
{
	float valueFront;
	float valueRight;
	float valueLeft;
	
	Paint paintFront;
	Paint paintLeft;
	Paint paintRight;
	Paint paintEdge;
	
	RectF boundingBox;
	
	
	public void SetValues(float front, float left, float right)
	{
		valueFront = front;
		valueLeft = left;
		valueRight = right;
		
		paintFront.setColor(GetColor(valueFront));
		paintLeft.setColor(GetColor(valueLeft));
		paintRight.setColor(GetColor(valueRight));
	}
	
	private int GetColor(float value)
	{
		//clip values between 0 and 255
		if(value > 255)
			value = 255;
		if(value < 0)
			value = 0;
		
		int red = (int)((value < 0? -value:value)/255f*510);
		int grn = 510-(int)((value < 0? -value:value)/255f*510);
		
		if(grn > 255)
			grn = 255;
		if(red > 255)
			red = 255;
		
		return Color.rgb(red, grn, 0);
	}
	
	public MazeSensorView(Context context)
	{
		this(context, null);
	}

	public MazeSensorView(Context context, AttributeSet attrs)
	{
		this(context, attrs, 0);
	}

	public MazeSensorView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		boundingBox = new RectF(0,0,100,100);
		
		paintFront = new Paint(Paint.ANTI_ALIAS_FLAG);
		paintFront.setStyle(Style.FILL);
		
		paintRight = new Paint(Paint.ANTI_ALIAS_FLAG);
		paintRight.setStyle(Style.FILL);
		
		paintLeft = new Paint(Paint.ANTI_ALIAS_FLAG);
		paintLeft.setStyle(Style.FILL);
		
		paintEdge = new Paint(Paint.ANTI_ALIAS_FLAG);
		paintEdge.setStyle(Style.STROKE);
		
		SetValues(128,128,128);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh)
	{
		super.onSizeChanged(w, h, oldw, oldh);
		boundingBox = new RectF(0,0,w,h);
	}

	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		
		canvas.drawArc(boundingBox, 135, 90, true, paintRight);
		canvas.drawArc(boundingBox, 225, 90, true, paintFront);
		canvas.drawArc(boundingBox, 315, 90, true, paintLeft);
		
		canvas.drawArc(boundingBox, 135, 90, true, paintEdge);
		canvas.drawArc(boundingBox, 315, 90, true, paintEdge);
		canvas.drawArc(boundingBox, 0, 360, true, paintEdge);
		
	}
}
